/* 
 * Copyright 2012 Google Inc. All Rights Reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *     http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.appspot.dispotrains.station;

import com.google.common.collect.Lists;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.appspot.dispotrains.R;

import java.text.DateFormat;
import java.util.Date;
import java.util.List;

public class ElevatorAdapter extends BaseAdapter {
  private class ViewHolder {
    TextView tvSituation;
    TextView tvDirection;
    TextView tvStatus;
    TextView tvDate;
    View container;
  }

  private final Station station;
  private final List<Elevator> elevators;
  private final LayoutInflater inflater;

  public ElevatorAdapter(Context context, Station station) {
    this.inflater = LayoutInflater.from(context);
    this.station = station;
    this.elevators = Lists.newArrayList(station.getElevators());
  }

  @Override
  public int getCount() {
    return elevators.size();
  }

  @Override
  public Object getItem(int position) {
    return elevators.get(position);
  }

  @Override
  public long getItemId(int position) {
    return elevators.get(position).hashCode();
  }

  @Override
  public View getView(int position, View convertView, ViewGroup parent) {
    ViewHolder holder;
    if (convertView == null) {
      holder = new ViewHolder();
      convertView = inflater.inflate(R.layout.elevator, null);
      holder.container = convertView;
      holder.tvSituation = (TextView) convertView.findViewById(R.id.elevatorName);
      holder.tvStatus = (TextView) convertView.findViewById(R.id.elevatorStatus);
      holder.tvDirection = (TextView) convertView.findViewById(R.id.elevatorDirection);
      holder.tvDate = (TextView) convertView.findViewById(R.id.elevatorStatusDate);
      convertView.setTag(holder);
    } else {
      holder = (ViewHolder) convertView.getTag();
    }

    holder.tvSituation.setText(elevators.get(position).situation);
    holder.tvStatus.setText(elevators.get(position).status);
    if (!elevators.get(position).status.equals("Disponible")) {
      holder.container.setBackgroundColor(0xFFFFFF88);
    }
    holder.tvDirection.setText(elevators.get(position).direction);
    Date date = elevators.get(position).date;
    DateFormat formatter = DateFormat.getDateInstance();
    holder.tvDate.setText(formatter.format(date));
    return convertView;
  }
}